package org.one.stone.soup.grfxML.plugin.swing;

import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JSpinner;
import javax.swing.ListCellRenderer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.one.stone.soup.grfxML.DataFont;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class JFontListPlugin extends SimplePlugin implements Runnable{
	private static final int MESSAGE_ARG = 0;
	private static final int TARGET_ARG = 1;
	private static final int ENTER_ARG = 2;

	private DataString message = new DataString();
	private DataFont target = new DataFont();
	private DataState enter = new DataState();

	public class JFontList extends JDialog implements ListCellRenderer,ItemListener,ChangeListener,ActionListener
	{
		private JComboBox list;
		private JFontListPlugin parent;
		private JSpinner spinner;

		private JCheckBox bold;
		private JCheckBox italic;

		public JFontList(JFontListPlugin parent,Font[] fonts,int pointSize)
		{
			this.parent = parent;

			Object[] options = new Object[fonts.length];
			for(int loop=0;loop<fonts.length;loop++)
			{
				Font font = new Font( fonts[loop].getFontName(),Font.PLAIN,12 );
				JLabel label = new JLabel(font.getFontName());
				label.setFont( font );
				options[loop] = label;
			}

			Container pane = getContentPane();
			pane.setLayout(new GridBagLayout());

			GridBagConstraints cell = new GridBagConstraints();
			cell.gridx = 0;
			cell.gridy = 0;
			cell.gridwidth = 3;
			list = new JComboBox(options);
			list.setRenderer( this );
			list.addItemListener( this );
			pane.add( list,cell );

			cell = new GridBagConstraints();
			cell.gridx = 0;
			cell.gridy = 1;
			spinner = new JSpinner();
			spinner.setValue( new Integer(pointSize) );
			spinner.addChangeListener(this);
			pane.add( spinner,cell );

			cell = new GridBagConstraints();
			cell.gridx = 1;
			cell.gridy = 1;
			JCheckBox option = new JCheckBox("Bold");
			option.setActionCommand("BOLD");
			pane.add( option,cell );
			option.addActionListener(this);
			bold = option;

			cell = new GridBagConstraints();
			cell.gridx = 2;
			cell.gridy = 1;
			option = new JCheckBox("Italic");
			option.setActionCommand("Italic");
			pane.add( option,cell );
			option.addActionListener(this);
			italic = option;

			setModal(true);
			pane.doLayout();
			pack();
			setResizable(false);
		}

		public void itemStateChanged(ItemEvent ie)
		{
			parent.setFontFace( ((JLabel)list.getSelectedItem()).getText() );
		}

		public void stateChanged(ChangeEvent ce)
		{
			try{
				Integer newValue = (Integer)spinner.getValue();

				if(newValue!=null)
				{
					parent.setPointSize( newValue.intValue() );
				}
			}catch(Exception e)
			{}
		}

		public void actionPerformed(ActionEvent e)
		{
			String value="";
			if(bold.isSelected())
			{
				value+="BOLD";
			}
			if(italic.isSelected())
			{
				value+="ITALIC";
			}

			parent.setFontStyle(value);
		}

		public Component getListCellRendererComponent(
			JList list,
			Object value,
			int index,
			boolean isSelected,
			boolean cellHasFocus)
		{
			JLabel label = (JLabel)value;
			label.setToolTipText(label.getText());

			if(isSelected)
			{
				label.setFont( new Font( label.getText(),Font.BOLD,14 ) );
			}
			else
			{
				label.setFont( new Font( label.getText(),Font.PLAIN,12 ) );
			}

			return label;
		}
	}


/**
 * FileToText constructor comment.
 */
public JFontListPlugin(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(enter.getValue()==true)
	{
		new Thread(this).start();
		enter.setValue(false,this);
	}
 }catch(LockException le)
 {}
}

public void run()
{
		Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		JFontList list = new JFontList(this,fonts,target.getPointSize().getValue());
		list.setVisible(true);
}

public void setFontFace(String newValue)
{
	try{
		if(newValue!=null)
		{
			target.getFace().setValue( newValue,this );
		}
	}catch(LockException le)
	{}
}

public void setPointSize(int newValue)
{
	try{
		target.getPointSize().setValue( newValue,this );
	}catch(LockException le)
	{}
}


public void setFontStyle(String newValue)
{
	try{
		target.getStyle().setValue( newValue,this );
	}catch(LockException le)
	{}
}

/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	message = grfxMLCaster.cast(message,getArg(message,MESSAGE_ARG,store));
	target = grfxMLCaster.cast(target,getArg(target,TARGET_ARG,store));
	enter = grfxMLCaster.cast(enter,getArg(enter,ENTER_ARG,store));
}
public void replace(Field oldObj, Field newObj)
{
	message = grfxMLCaster.replace(message,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	enter = grfxMLCaster.replace(enter,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}

}
